package m2;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

public class Producer {
    static String[] msgs = {
            "15103111039,创建",
            "15103111065,创建",
            "15103111039,付款",
            "15103117235,创建",
            "15103111065,付款",
            "15103117235,付款",
            "15103111065,完成",
            "15103111039,推送",
            "15103117235,完成",
            "15103111039,完成"
    };

    public static void main(String[] args) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        //创建生产者
        DefaultMQProducer p = new DefaultMQProducer("p2");
        //设置name server
        p.setNamesrvAddr("192.168.64.141:9876");
        //启动
        p.start();
        //遍历msgs数组发送消息,通过队列选择器来选择队列
        for (String s :msgs){
            // s = "15103111039,完成"
            Long orderId = Long.valueOf(s.split(",")[0]);
            Message msg = new Message("Topic2", s.getBytes());
            //p.send(msg, 队列的选择器,选择依据);
            SendResult r =
                p.send(msg, (mqs,msg1,arg) -> {
                    //参数: 服务器队列的列表,消息,选择依据
                    Long id = (Long) arg;
                    int index = (int) (id % mqs.size());
                    return mqs.get(index);
                },orderId);
            System.out.println(r);
        }
    }
}
